package com.i2works.smartluolong.web.entity.common;

import com.google.common.collect.Lists;
import com.i2works.smartluolong.web.entity.user.UserResDO;
import com.i2works.smartluolong.utils.entity.Tree;

import java.util.List;

/**
 * 树状工具类
* @author koal koal@vip.qq.com
* @date 2015年11月7日 下午4:15:25 
*
 */
public class TreeUtil {

	/**
	 * 获取左侧资源菜单
	* @author koal
	* @param pid
	* @param treeDataList
	* @return
	* @return List<Tree>
	* @throws
	 */

	public static List<Tree> getNodeTree(long pid , List<Tree> treeDataList) {
        List<Tree> newTreeDataList = Lists.newArrayList();
        for (Tree tree : treeDataList) {  
            //子节点
            if(tree.getPid() == pid){
                //递归获取子节点下的子节点 
            	tree.setChildren(getNodeTree(tree.getId(), treeDataList));  
            	newTreeDataList.add(tree);
			}
        }
        return newTreeDataList;
    }


	/**
	 * 生成过滤后的树结构
	 * @param res 其中userRes和adminRes数据结构一样，可通用
     */
	public static Tree resTree(UserResDO res) throws Exception {
			Tree tree =  new Tree();
			tree.setId(res.getId());
			tree.setPid(res.getPid());
			if (res.getStatus() == 0) {
				tree.setText(res.getResName() + "『禁』");
			} else {
				tree.setText(res.getResName());
			}
			tree.setType(res.getResType());
			tree.setSort(res.getSort());
			tree.setStatus(res.getStatus());
			tree.setUrl(res.getResCode());
		return tree;
	}


}
